/*
 * CameraScreen.java
 *
 * Created on March 16, 2006, 2:59 PM
 *
 * To change this template, choose Tools | Options and locate the template under
 * the Source Creation and Management node. Right-click the template and choose
 * Open. You can then make changes to the template in the Source Editor.
 */

package Media;

import Comm.Util.Logger;
import Experience.*;
import Experience.Screens.ChoiceScreen;
import javax.microedition.lcdui.*;
import javax.microedition.midlet.MIDlet;

import java.io.IOException;

import java.util.Vector;


/**
 *
 * @author dmgreen
 */
public class CameraScreen extends ChoiceScreen {
    
    private MIDlet myMIDlet = null;
    private CameraMgr myCam = null;
    
    private static final int PLAYBACK = 1;
    private static final int  CAPTURE = 2;
    
    int MODE = CAPTURE;
    
    int INSTRUCTIONS_START_Y = 0;
    int VFINDER_START_Y      = 100; //useda be 50
    int BUTTONS_START_Y      = -1; 
    
    boolean NEXT_OK = true;
    
    private Image curImage = null;
    
    private Image snapImage,moreImage;
    
    private int icon_x;
    private int icon_y;
    private int icon_anchor;
    
    /** Creates a new instance of CameraScreen */
    public CameraScreen(MIDlet expsamp) {
        
        super(expsamp, "use trigger button to take a picture", 0, "", "Next");
        
        //VFINDER_START_Y = super.getMsgYPos();
        //VFINDER_START_Y = 40;
        
        BUTTONS_START_Y = getHeight() - largeHeight;
     
        icon_x = getWidth()/2-10;
        icon_y = getHeight()-5;
        icon_anchor = Graphics.BOTTOM | Graphics.LEFT;
        
        myMIDlet = expsamp;
        
        try {
            snapImage = Image.createImage("/snapshot_ico.png");
            moreImage = Image.createImage("/snapshot-more_ico.png");            
        }
        catch (IOException ioe) {
            Logger.getLog().addLogMessage("Graphic NOT FOUND", Logger.MSG_ERROR);
        }
        
        myCam = new CameraMgr((Canvas)this);
        myCam.showViewfinder(VFINDER_START_Y);
    }
    
    public void paint (Graphics g) {
        
        //wipes the viewfinder or image, whichever is there ...
        wipeContents(g);
        
        if (MODE == CAPTURE) {
            setText("Press the trigger button to take picture");
            setLeftLabel("");
            setRightLabel("Next");
            super.paint(g);
            g.drawImage(snapImage, icon_x, icon_y, icon_anchor);
        }
        else if (MODE == PLAYBACK) {
            g.drawImage(curImage, 0, VFINDER_START_Y, Graphics.TOP | Graphics.LEFT);
            setText("Press the trigger button to take another picture");     
            setLeftLabel("Delete");
            setRightLabel("Next");
            super.paint(g);
            g.drawImage(moreImage, icon_x, icon_y, icon_anchor);
        }
    }
    
    public void showViewfinder() {
        myCam.showViewfinder(VFINDER_START_Y);
    }
    
    private void wipeContents(Graphics g) {
        int color = g.getColor();
        g.setColor(255,255,255);
        //g.fillRect(0, VFINDER_START_Y, getWidth(), (BUTTONS_START_Y - VFINDER_START_Y) );
        g.fillRect(0, 0, getWidth(), getHeight());
        g.setColor(color);
    }
    
    public void disable() {
        NEXT_OK = false;
    }
    
    public void enable() {
        NEXT_OK = true;
    }
    
    protected void keyReleased(int keyCode) {
        super.keyReleased(keyCode);
        int action = getGameAction(keyCode);
        
        if (action == 8) {
           
            if (MODE == CAPTURE) {
              byte[] img_array = myCam.takeSnapshot();
              
              if (img_array != null) {
                  ((ExpUI) myMIDlet).addImageToSample(img_array);
                  Image image = Image.createImage(img_array, 0, img_array.length);
                  curImage = image;
                  myCam.hideViewfinder();
                  //setLeftLabel("Delete");
                  MODE = PLAYBACK;
              }
           }
            else if (MODE == PLAYBACK) {
                myCam.showViewfinder(VFINDER_START_Y);
                MODE = CAPTURE;
                //setLeftLabel("");
            } 
            repaint();
            
        }
    }
    
    public void cleanUp () {
        if (myCam != null) {
            myCam.hideViewfinder();
            
            MODE = CAPTURE;
            //setLeftLabel("");
            //setRightLabel("Next");
            repaint();
        }        
    }
    
    protected void leftPressed() {
        if (MODE == PLAYBACK) {
            //left label is delete
            ((ExpUI) myMIDlet).deleteLastImage();
            setLeftLabel("");
            myCam.showViewfinder(VFINDER_START_Y);
            MODE = CAPTURE;
            repaint();
        }
        
    }
        
    protected void rightPressed() {
        
        if (!NEXT_OK) {
            Logger.getLog().addLogMessage("Discarding 'Next' - camera not ready!", Logger.MSG_DEBUG);
            return;
        }
        
        if ( ((ExpUI)myMIDlet).display.getCurrent() != this) {return;}
        
        ((ExpUI) myMIDlet).notifyNext("");
    } 
}
